﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using nwrole.lib;
using System.IO;

namespace nwrole.console
{
    public class IntegrationCommands : CommandBase
    {
        public IntegrationCommands()
        {
            if (!Variables.ContainsKey("supervisor-host"))
                Variables["supervisor-host"] = "localhost";
        }

        #region Supervisor
        /*[Command("install-supervisor")]
        public void InstallSupervisor ([Optional(null)] string username, [Optional(null)] string password)
        {
            if (SupervisorService.IsInstalled)
                this.PrintLine(ConsoleColor.Red, "Supervisor service is already installed.");
            else
                this.VerifyStep("Installing supervisor service", () => SupervisorService.InstallSupervisor(username, password));
        }

        [Command("uninstall-supervisor")]
        public void UninstallSupervisor ()
        {
            if (!SupervisorService.IsInstalled)
                this.PrintLine(ConsoleColor.Red, "Supervisor service is not installed.");
            else if (SupervisorService.IsRunning)
                this.PrintLine(ConsoleColor.Red, "Supervisor service is running. Run stop-supervisor before uninstalling.");
            else
                this.VerifyStep("Uninstalling supervisor service", () => SupervisorService.UninstallSupervisor());
        }*/

        [Command("start-supervisor")]
        public void StartSupervisor ()
        {
            if (!Supervisor.IsWinServiceInstalled)
                this.PrintLine(ConsoleColor.Red, "Supervisor service is not installed.");
            else if (Supervisor.IsWinServiceRunning)
                this.PrintLine(ConsoleColor.Red, "Supervisor service is already running.");
            else
                this.VerifyStep("Starting supervisor service", () => Supervisor.StartWinService());
        }

        [Command("stop-supervisor")]
        public void StopSupervisor ()
        {
            if (!Supervisor.IsWinServiceInstalled)
                this.PrintLine(ConsoleColor.Red, "Supervisor service is not installed.");
            else if (!Supervisor.IsWinServiceRunning)
                this.PrintLine(ConsoleColor.Red, "Supervisor service is not running.");
            else
                this.VerifyStep("Stopping supervisor service", () => Supervisor.StopWinService());
        }

        [Command("supervisor-status")]
        public void SupervisorStatus ()
        {
            this.PrintKeyValue("Installed", Supervisor.IsWinServiceInstalled);
            this.PrintKeyValue("Running", Supervisor.IsWinServiceRunning);
        }

        /*[Command("update-supervisor")]
        public void UpdateSupervisor()
        {
            if (SupervisorService.IsRunning)
                this.VerifyStep("Stopping supervisor service", () => SupervisorService.StopSupervisor());
            this.VerifyStep("Updating supervisor service", () => SupervisorService.UpdateSupervisor());
            this.VerifyStep("Starting supervisor service", () => SupervisorService.StartSupervisor());
        }*/
        #endregion
        
        #region Services
        [Command("list-services")]
        public void ListService()
        {
            ServiceData[] serviceDatas = null;
            this.VerifyStep("Listing services", () => {
                using (var supervisor = new TcpChannel<ISupervisorContract>((Variables["supervisor-host"] as string) + ":" + 8094 + "/Supervisor"/*, Variables["supervisor-username"] as string, Variables["supervisor-password"] as string*/)) {
                    return ((serviceDatas = supervisor.Channel.GetServices()) != null);
                }
            });
            if (serviceDatas != null)
                this.PrintObject(serviceDatas);
        }

        #endregion
    }
}
